//
//  OpenAppCenter.swift
//  App_Common
//
//  Created by 蒋晨成 on 2021/12/24.
//

import Foundation
import Standard
import StoreKit
import UICore
import UIKit

enum OpenAppCenter {}

extension OpenAppCenter: Actable {
    static func act(values: [String: Any]) -> Bool {
        guard let schemeEncodeString = values["scheme"] as? String,
              let scheme = schemeEncodeString.urlDecode(),
              let openScheme = URL(string: scheme),
              let urlEncodeString = values["url"] as? String,
              let url = urlEncodeString.urlDecode(),
              let openURL = URL(string: url)
        else {
            return false
        }
        if Application.canOpen(url: openScheme) {
            Router.open(url: openScheme)
        } else {
            switch openURL.scheme {
            case .none:
                Router.open(pattern: openURL.absoluteString)
            case .some:
                Router.open(url: openURL)
            }
        }
        return true
    }

    static var routePattern: String = "openapp/<string:scheme>/<string:url>"
}
